package com.example.yt_app.Activity;

import android.os.Build;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;

import com.example.yt_app.Activity.BaseActivity.SwipeBackBaseActivity;
import com.example.yt_app.AppManager;
import com.example.yt_app.R;
import com.example.yt_app.Urils.State;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import butterknife.BindView;
import me.imid.swipebacklayout.lib.SwipeBackLayout;

/**
 * Created by dell on 2021/8/3.
 */


/**
 *
 * @author dell
 * @date 2021/7/24
 */
@SuppressWarnings("ALL")
public class SaveActivity  extends SwipeBackBaseActivity {

    @SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
    @BindView(R.id.weathers)
    WebView Weather;
    @BindView(R.id.detail_toolbar)
    Toolbar toolbar;


    @Override
    protected SwipeBackLayout getSwipeBackLayout(int layoutId) {
        return null;
    }
    @Override
    protected int getLayoutId() {
        return R.layout.weatherwebview;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(toolbar).init();
        //隐藏状态栏
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init();

    }
    @Override
    protected void initView() {
        AppManager.getInstance().addActivity(this);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        /*设置web基础*/
        initWebSettings();
        /*设置web链接*/
        initWebView();


    }
    /**
     * 设置web链接
     * */
    private void initWebView() {
        String url = "https://www.2344ec68ad1b.com/index/home.html";
        /*设置web基础*/
        Weather.setWebViewClient(new WebViewClient());
        Weather.loadUrl(url);
    }
    /**
     * 设置web基础
     * */
    @SuppressWarnings({"AliDeprecation", "AlibabaUndefineMagicConstant", "AlibabaRemoveCommentedCode"})
    private void initWebSettings() {
        WebSettings settings = Weather.getSettings();
        //支持获取手势焦点
        Weather.requestFocusFromTouch();
        //支持JS
        settings.setJavaScriptEnabled(true);
        //支持插件
        //noinspection AliDeprecation
        settings.setPluginState(WebSettings.PluginState.ON);
        //设置适应屏幕
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        //支持缩放
        settings.setSupportZoom(false);
        //隐藏原生的缩放控件
        settings.setDisplayZoomControls(false);
        //支持内容重新布局
        //noinspection AliDeprecation
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.supportMultipleWindows();
        settings.setSupportMultipleWindows(true);
        //设置缓存模式
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        //noinspection AliDeprecation
        settings.setAppCacheEnabled(true);
//        settings.setAppCachePath(Weather.getContext().getCacheDir().getAbsolutePath());
        //设置可访问文件
        settings.setAllowFileAccess(true);
        //当webview调用requestFocus时为webview设置节点
        settings.setNeedInitialFocus(true);
        //支持自动加载图片
        //noinspection AlibabaUndefineMagicConstant
        if (Build.VERSION.SDK_INT >= 19) {
            settings.setLoadsImagesAutomatically(true);
        } else {
            settings.setLoadsImagesAutomatically(false);
        }
        settings.setNeedInitialFocus(true);
        //设置编码格式
        settings.setDefaultTextEncodingName("UTF-8");

    }
    /**
     * 设置web链接设置
     * */


    @Override
    protected void initData() {

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        State.OTHER = true;
    }

    @Override
    protected void setListener() {
        //toolbar返回按钮监听
        toolbar.setNavigationOnClickListener(v ->finish() );


    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(keyCode==KeyEvent.KEYCODE_BREAK){
            // 判断当前页面是否处于首部
            if(Weather.canGoBack()){
                // 返回上一页面
                Weather.goBack();
                return true;
            }else{
                // 如果当前页面是第一个页面
                System.exit(0); // 退出程序
            }
        }

        return super.onKeyDown(keyCode, event);

    }

}
